package com.xique.door.controller;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.vo.user.UserExcelVo;
import com.xique.door.service.IUserExceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserExceController {
  @Autowired private IUserExceService userExceService;
  /** 初始化导入 */
  @PostMapping("/initialization")
  public AjaxResult initializationData(MultipartFile file) throws Exception {
    ExcelUtil<UserExcelVo> util = new ExcelUtil<>(UserExcelVo.class);
    List<UserExcelVo> userExcelVos = util.importExcel(file.getInputStream());
    if (CollectionUtils.isEmpty(userExcelVos)) {
      return AjaxResult.error("excel数据不能为空");
    }
    String message = userExceService.initialization(SecurityUtils.getParkId(), userExcelVos);
    return AjaxResult.success(message);
  }
  @GetMapping("/dowmInitializationTemplate")
  public void importTemplate(HttpServletResponse response) throws IOException {
    List<UserExcelVo> userList = new ArrayList<>();
    //导入模板案例数据
    UserExcelVo userVo = new UserExcelVo();
    userVo.setNumber("1");
    userVo.setAreaName("东区");
    userVo.setBuildingName("1幢1单元");
    userVo.setHouseCode("0102");
    userVo.setUserName("住户名称");
    userVo.setSex("男");
    userVo.setPhone("18888888888");
    userVo.setIdentityType("0");
    userList.add(userVo);
    ExcelUtil<UserExcelVo> util = new ExcelUtil<>(UserExcelVo.class);
    util.exportExcel(response, userList, "门禁用户数据");
  }
}
